#include "MsgHtmlObj.h"
#include <QFile>
#include <QMessageBox>
//#include "CCMainWindow.h"

extern QString gstrLoginHeadPath;

MsgHtmlObj::MsgHtmlObj(QObject *parent, QString msgLPicPath)
	: QObject(parent),
	m_msgLPicPath(msgLPicPath)
{
	initHtmlTmpl();
	//m_msgLHtmlTmpl = getMsgTmplHtml("msgleftTmpl");
	//m_msgRHtmlTmpl = getMsgTmplHtml("msgrightTmpl");
	//:/Resources/MainWindow/MsgHtml/msgrightTmpl.html
	//:/Resources/MainWindow/MsgHtml/msgleftTmpl.html
}

MsgHtmlObj::~MsgHtmlObj()
{
}

void MsgHtmlObj::initHtmlTmpl()
{
	m_msgLHtmlTmpl = getMsgTmplHtml("msgleftTmpl");
	m_msgLHtmlTmpl.replace(
		"qrc:/Resources/MainWindow/girl.png",
		m_msgLPicPath
	);
		
	m_msgRHtmlTmpl = getMsgTmplHtml("msgrightTmpl");
	m_msgRHtmlTmpl.replace(
		"qrc:/Resources/MainWindow/girl.png",
		gstrLoginHeadPath
	);
}

QString MsgHtmlObj::getMsgTmplHtml(const QString & code)
{
	//QFile file(QString::asprintf(":/Resources/MainWindow/MsgHtml/%s.html", code));
	QString s = QString::asprintf(":/Resources/MainWindow/MsgHtml/%s.html", code);
	QString s1 = QString(":/Resources/MainWindow/MsgHtml/%s.html").arg(code);

	QFile file(QString(":/Resources/MainWindow/MsgHtml/%1.html").arg(code));
	file.open(QFile::ReadOnly);
	QString strData;

	if (file.isOpen())
	{
		// 全部一次性读取
		strData = QLatin1String(file.readAll());
	}
	else
	{
		// information（父窗体指针，标题，提示内容）;
		// 因为当前写代码的对象，是从 QObject 派生来的，不是从QWidget派生来的
		// 因此要写 空
		QMessageBox::information(nullptr, "Tips", "Failed to init html!");
		file.close();
		return QString("");			// 返回空
	}

	file.close();

	return strData;
}

